<?php
session_start();
include_once $_SERVER['DOCUMENT_ROOT']."/math-videos/configure.inc.php";
include_once $site_path."/header.php";

$display_list ='';
$sqltext ='';
$limitvalue=0;
$limit=10;
$click_search_button_flag=false;
if($_GET['page']!='')
{
	$cur_page = (int)$_GET['page'];
	$limitvalue = $limit*($cur_page-1);
}
$maxvalue = $limitvalue+$limit;
if(isset($_POST['search']) && strtoupper($_POST['search']) == 'SEARCH')
{
	$_SESSION['institutes'] =$_POST['institutes'];
	$_SESSION['keyword_content'] =$_POST['keyword_content'];
	$_SESSION['keyword'] =$_POST['keyword'];
	$click_search_button_flag=true;
}
$inst_where ='';
$search_by_fields='';
$search_by_company='';
if($_SESSION['institutes'] !='' && $_SESSION['keyword_content']=='')
{
	 $institute_id = $_SESSION['institutes'];
	 $search_by_company = $institute_id;
	// $inst_where = " and institute_id='$institute_id'";
	 $sqltext="select  id,
										title, 
										abstract, 
										to_char(asset.lecture_date,'mm/dd/yyyy') as lecture_date,  
										duration, 
										asset_type, 
										abstract_url, 
										remote_url, 
										location, 
										event_title,
										rating, 
										views, 
										thumbnail,
										thumbnail_type,
										institute_id
						from asset
						where institute_id='$institute_id'
						order by id DESC
						";

	$params = array();
	$result = db_select_query($conn,$sqltext,$params);
}
else
{
	if($_SESSION['institutes'] !='' && $_SESSION['keyword_content']!='')
	{
		 $institute_id = $_SESSION['institutes'];
		 $inst_where = " and institute_id='$institute_id'";
		 $search_by_company = $institute_id;
	}
	if($_SESSION['keyword_content'] !='')
	{
		if($_SESSION['keyword']!="" && $_SESSION['keyword']==1)//keyword
		{
			$search_by_fields .="keyword|".$_SESSION['keyword_content'];
			$sqltext="select  id,
												title, 
												abstract, 
												to_char(asset.lecture_date,'mm/dd/yyyy') as lecture_date,  
												duration, 
												asset_type, 
												abstract_url, 
												remote_url, 
												location, 
												event_title,
												rating, 
												views, 
												thumbnail,
												thumbnail_type,
												institute_id
								from asset
								where (id in (select asset_id from keyword  WHERE keyword ilike ?) 
								or event_title ilike ? or abstract ilike ? ) $inst_where
								order by id DESC
								";
	
			$params = array();
			array_push($params,sql_escape("%".$_SESSION['keyword_content']."%"));
			array_push($params,sql_escape("%".$_SESSION['keyword_content']."%"));
			array_push($params,sql_escape("%".$_SESSION['keyword_content']."%"));
			
			$result = db_select_query($conn,$sqltext,$params);
		//	$where .= "(select asset_id from keyword  WHERE keyword ilike ?)";
			//array_push($params,sql_escape("%".$_POST['keyword']."%"));
		}
		elseif($_SESSION['keyword']!="" && $_SESSION['keyword']==2)//author
		{
			$search_by_fields .="author|".$_SESSION['keyword_content'];
			$sqltext="select  id,
												title, 
												abstract, 
												to_char(asset.lecture_date,'mm/dd/yyyy') as lecture_date,  
												duration, 
												asset_type, 
												abstract_url, 
												remote_url, 
												location, 
												event_title,
												rating, 
												views, 
												thumbnail,
												thumbnail_type,
												institute_id
								from asset
								where id in (select asset_id from presenter  WHERE first_name ilike ? or last_name ilike ?) $inst_where
								order by id DESC
								";
			//$sqltext="select asset_id from keyword  WHERE keyword like '%".$_POST['keyword']."%'";
		//	echo $sqltext."<br>";
			$params = array();
			array_push($params,sql_escape("%".$_SESSION['keyword_content']."%"));
			array_push($params,sql_escape("%".$_SESSION['keyword_content']."%"));
			$result = db_select_query($conn,$sqltext,$params);
		}
		elseif($_SESSION['keyword']!="" && $_SESSION['keyword']==3)//title
		{
			$search_by_fields .="title|".$_SESSION['keyword_content'];
			$sqltext="select  id,
												title, 
												abstract, 
												to_char(asset.lecture_date,'mm/dd/yyyy') as lecture_date,  
												duration, 
												asset_type, 
												abstract_url, 
												remote_url, 
												location, 
												event_title,
												rating, 
												views, 
												thumbnail,
												thumbnail_type,
												institute_id
								from asset
								where title ilike ?  $inst_where
								order by id DESC
								";
			//$sqltext="select asset_id from keyword  WHERE keyword like '%".$_POST['keyword']."%'";
		//	echo $sqltext."<br>";
			$params = array();
			array_push($params,sql_escape("%".$_SESSION['keyword_content']."%"));
			$result = db_select_query($conn,$sqltext,$params);
		}
	}
	if($_SESSION['institutes'] =='' && $_SESSION['keyword_content']=='')
	{
		$sqltext="select  id,
												title, 
												abstract, 
												to_char(asset.lecture_date,'mm/dd/yyyy') as lecture_date,  
												duration, 
												asset_type, 
												abstract_url, 
												remote_url, 
												location, 
												event_title,
												rating, 
												views, 
												thumbnail,
												thumbnail_type,
												institute_id
								from asset
								order by id DESC limit 6
								";
	
			$params = array();
			$result = db_select_query($conn,$sqltext,$params);
	}
}
if($sqltext !='')
{
	$num_i=0;
	unset($array_list);
	while($row = db_fetch_object($result))
	{
		$array_list[]=$row;			
	}
	if(isset($array_list))
	{
		$items_num=count($array_list);
	}
	else
	{
		 $items_num=0;
	}
	$limitvalue=0;
	if($items_num>$limit)	
	{
		$pagename = "index.php?";		
		if($cur_page !='')
		{
			$limitvalue = $limit*($cur_page-1);
		}		
	
		echo "<div align='right' >";
		paginate($items_num,$limit,$cur_page,"#336699");
		echo "</div>";
	}
	if(!isset($array_list))
	{
		$display_list .= "<table cellspacing='0' cellpadding='3' width='95%' style='background-color:#efefef;border:1px solid #ccc;padding:4px;margin:10px;'>";
		$display_list.="<tr><td valign='top' width='130' colspan='2'> No video found. </td></tr></table>";
	}
	else
	{
		for($k=$limitvalue; $k<count($array_list);$k++)			
	//	for($k=0; $k<count($arry_list); $k++)
		{
			$display_list .= "<table cellspacing='0' cellpadding='3' width='95%' style='background-color:#efefef;border:1px solid #ccc;padding:4px;margin:10px;'>";
			foreach ($array_list[$k] as $key => $value) 
			{
				$value = trim($value);
				$$key = $value;
				$result_array[$num_i][$key]=$value;	
				//	echo "$$key=$value<br>";	
			}
			$num_i++;
			//insert to search log
			if($click_search_button_flag)
			{
				$params = array();
				array_push($params,sql_escape($id));
				$sqltext="INSERT INTO search_result(asset_id,";
				$sqlvalue=" values(?,";
				if($search_by_company !=='')
				{
					$sqltext .="search_by_company,";
					$sqlvalue .=" ?,";
					array_push($params,sql_escape($search_by_company));
				}
				if($search_by_fields !=='')
				{
					$sqltext .="search_by_fields,";
					$sqlvalue .=" ?,";
					array_push($params,sql_escape($search_by_fields));
				}
				$sqltext .="search_date)";
				$sqlvalue .=" ?)";
				$sqltext .= $sqlvalue;
				
				array_push($params,date("Y-m-d H:i:s"));
				db_change_query($conn,$sqltext,$params);
			}
			//$content=	file_get_contents($_SERVER['DOCUMENT_ROOT']."/math-videos/images/".$thumbnail);
			//$content=	file_get_contents($site_http."images/".$thumbnail);
			//$content=	file_get_contents("http://www.caraburo.com/math-videos/images/3/13/13_thumb.jpg");
			//echo $site_http."images/".$thumbnail."<br>";
			//echo $content."<br>";
			//echo '<img src="data:image/".$thumbnail_type.";base64,' .$content. '" />';
		
			$display_list.="<tr><td valign='top' width='130'>";
			$display_list .= '<img src="'.$site_http.'images/'.$institute_id.'/'.$id.'/'.$id.'_thumb.'.$thumbnail_type.'" style="width:120px;height:90px;border:1px solid #ccc;margin:10px;" align="left" />';
			$display_list.="</td><td valign='top'><b>$title</b><br> ".date("F d, Y",strtotime($lecture_date))." <br> <i>$location</i> ";
			if($duration !=0)
			{
				$the_hour = 0;
				$the_min = 0;
				$the_sec = 0;
				if($duration >3600)
				{
					$the_hour = floor($duration/3600);
				}
				$the_min = floor(($duration-3600*$the_hour)/60);
				$the_sec = $duration-3600*$the_hour - $the_min*60;
				$display_list .= " <br> ";
				if($the_hour>0)
				{
					if($the_hour>1)
					{
					 $display_list .= $the_hour." hrs";
					}
					else
					{
						$display_list .= $the_hour." hr";
					}
				}
				if($the_min>0)
				{
				 $display_list .= $the_min." mins";
				}
				if($the_sec>0)
				{
				 $display_list .= $the_sec." sec";
				}
			}
			$display_list .= "<br>";
			//$display_list .= '<img src="data:image/'.$thumbnail_type.';base64,' .$content. '" />';
			//echo '<img src="data:image/jpg;charset=utf-8;base64,' .$content. '" />';
		//	$display_list .= '<img src="data:image/jpg;base64,' .$content. '" style="width:120px;height:90px;border:1px solid #ccc;margin:10px;" align="left" />';
			//$display_list .= "$abstract <br>";
			//$display_list .= "$abstract_url <br>";
			if($remote_url !='')
			{
				$display_list .= "Remote url: <a href=\"view_result.php?id=$id&amp;rm=Y\" target=\"_blank\">$remote_url</a><!--<a href=\"$remote_url\" onclick=\"view_result('".$id."')\" >$remote_url</a>--> <br>";
			}
			if($abstract_url !='')
			{
				$display_list .= "Abstract url:  <a href=\"view_result.php?id=$id&amp;rm=N\" target=\"_blank\">$remote_url</a><!--<a href=\"$abstract_url\" onclick=\"view_result('".$id."')\">$abstract_url</a>--> <br>";
			}
		 $display_list.="<hr size='1' color='#cccccc'/>";	
		//get presenter
			$sqltext1="SELECT first_name, last_name/*, middle_name, alt_name, suffix, title, department, organization*/ FROM presenter  WHERE asset_id= ?";
			$params = array();
			array_push($params,sql_escape($id));
			$result1 = db_select_query($conn,$sqltext1,$params);
			$num=0;
			
			while($row1 = db_fetch_object($result1))
			{
				if($num==0)
				{
					$display_list .="Presenters: ";
				}
				foreach ($row1 as $key1 => $value1) 
				{
					$value1 = trim($value1);
					$$key1 = $value1;
					//$presenter_array[$key]=$value;
					$result_array[$num_i]["presenters"][$num][$key1]=$value1;
					
				}
				$display_list .="$first_name $last_name<br>";
				$num++;
			}
		
			//get docs
			$sqltext6="select saved_file_name, file_name, file_type,id
										from docs WHERE asset_id= ?";
			$params = array();
			array_push($params,sql_escape($id));
			$result6 = db_select_query($conn,$sqltext6,$params);
			$num=0;
			while($row6 = db_fetch_object($result6))
			{
				if($num==0)
				{
					$display_list .="<br>Docs:";
				}
				foreach ($row6 as $key6 => $value6) 
				{
					$value6 = trim($value6);
					$$key6 = $value6;
				
				}
				$display_list .="<a href=\"../../docs/".$institute_id."/".$asset_id."/".$saved_file_name.".".$file_type."'\">". $file_name.".".$file_type."</a><br>";
				$num++;
			}
				//get keywords
				
		/*	$sqltext2="SELECT keyword FROM keyword  WHERE asset_id= ?";
			$params = array();
			array_push($params,sql_escape($id));
			$result2 = db_select_query($conn,$sqltext2,$params);
			while($row2 = db_fetch_object($result2))
			{
				foreach ($row2 as $key2 => $value2) 
				{
					$value2 = trim($value2);
					//$$key = $value;
					$result_array[$key2]=$value2;
				}
			}*/
				//get timecode
			$sqltext3="SELECT timecode, timecode_type, data FROM timecode  WHERE asset_id= ?";
			$params = array();
			array_push($params,sql_escape($id));
			$result3 = db_select_query($conn,$sqltext3,$params);
			$num=0;
			while($row3 = db_fetch_object($result3))
			{
				if($num==0)
				{
						$display_list .="timecode: ";
				}
				foreach ($row3 as $key3 => $value3) 
				{
					$value3 = trim($value3);
					$$key3 = $value3;
					$result_array[$num_i]['timecode'][$num][$key3]=$value3;
						
				}
			 $display_list .="$timecode - $timecode_type- $data<br>";
				$num++;
			}
					//get msc_number
			$sqltext4="SELECT msc FROM msc_number  WHERE asset_id= ?";
			$params = array();
			array_push($params,sql_escape($id));
			$result4 = db_select_query($conn,$sqltext4,$params);
			$num=0;
			while($row4 = db_fetch_object($result4))
			{
				if($num==0)
				{
					$display_list .="mscNumbers: ";
				}
				foreach ($row4 as $key4 => $value4) 
				{
					$value4 = trim($value4);
					$$key4 = $value4;
					$result_array[$num_i][$key4]=$value4;
						
				}
				$display_list .=$msc.",";
				$num++;
			}
			$display_list.="</td></tr>";
			$display_list.="</table>";
			if($k>=$maxvalue)
			{
				break;
			}
		}
	}
}

?>
<center>
<form action='index.php' method='post'>
<br>
<div>
<table cellspacing='0' cellpadding='5'>
<tr><td align='left'>Institutes:</td><td><select name='institutes'>
<option value=''>--All Institutes</option>
<?php
$sqltext="select id , full_name from institutes  WHERE id>0";
$params = array();
$result = db_select_query($conn2,$sqltext,$params);
$user_id='';
while($row = db_fetch_array($result))
{
	$id = $row['id'];
	$institute_name = $row['full_name'];
	if(isset($_SESSION['institutes']) && $_SESSION['institutes'] == $id)
	{
		 $selected='SELECTED';
	}
	else
	{
		 $selected='';
	}
	echo "<option value='$id' $selected>$institute_name</option>";
}
?>
</select>
</td></tr>
<?php
$check1 = '';
$check2 = '';
$check3 = '';

if(isset($_SESSION['keyword']))
{
	if($_SESSION['keyword'] ==1)
	{
		$check1 = 'CHECKED';
	}
	if($_SESSION['keyword'] ==2)
	{
		$check2 = 'CHECKED';
	}
	if($_SESSION['keyword'] ==3)
	{
		$check3 = 'CHECKED';
	}
}
else
{
	$check1 = 'CHECKED';
}
if(isset($_SESSION['keyword_content']))
{
	$keyword_content=$_SESSION['keyword_content'];
}
else
{
	$keyword_content ='';
}
?>
<tr><td>Search by:</td><td align='left'>
<input type='radio' name='keyword' value='1'  <?=$check1?> /> <b>Keyword:</b> &nbsp;&nbsp;
<input type='radio' name='keyword' value='2'  <?=$check2?> /> <b>Author:</b> &nbsp;&nbsp;
<input type='radio' name='keyword' value='3' <?=$check3?> /> <b>Title:</b> </td></tr>

<tr><td width='80'>Search for:</td><td align='left'><input type='text' name='keyword_content' value='<?=$keyword_content?>' size='40' maxlength='80' />
&nbsp;&nbsp;<input type='submit' name='search' value='Search'></td></tr>
<!--<tr><td align='left' colspan='2' ><br/>
Note: Choose a search type, then enter any part of the keyword, author's name, the title and click Search button.<br /> </td></tr>
-->
</table>
</div>
</form>

<div  align='left'>
<?php
//echo json_encode($result_array);
echo $display_list;
//echo "{ <br />".displayTree($result_array)."<br/>}";

?>
</div>	
</center>
<?php

include "/footer.php";
?>
